package com.zt.rongda.stock.controller;

import com.zt.servlet.*;
import com.zt.rongda.stock.model.StockPurchaseDetail;
import com.zt.rongda.stock.service.StockPurchaseDetailService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping(value = "/stockPurchaseDetail")
public class StockPurchaseDetailController {

    @Autowired
    private StockPurchaseDetailService stockPurchaseDetailService;

    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(stockPurchaseDetailService.getTable(reqBean));
    }

    @RequestMapping(value = "/queryByListId")
    @ResponseBody
    public JSONObject queryByListId(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(stockPurchaseDetailService.queryByListId(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockPurchaseDetailService.add(getStockPurchaseDetail(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockPurchaseDetailService.update(getStockPurchaseDetail(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        stockPurchaseDetailService.delete(reqBean.getInfo("id"));
        return Response.responseSuccess();
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private StockPurchaseDetail getStockPurchaseDetail(ReqBean reqBean) {
        StockPurchaseDetail stockPurchaseDetail = new StockPurchaseDetail();
        stockPurchaseDetail.setId(reqBean.getInfo("id"));
        stockPurchaseDetail.setListId(reqBean.getInfo("listId"));
        stockPurchaseDetail.setStockId(reqBean.getInfo("stockId"));
        stockPurchaseDetail.setStockName(reqBean.getInfo("stockName"));
        stockPurchaseDetail.setTypeId(reqBean.getInfo("typeId"));
        stockPurchaseDetail.setTypeName(reqBean.getInfo("typeName"));
        stockPurchaseDetail.setSpec(reqBean.getInfo("spec"));
        stockPurchaseDetail.setBrand(reqBean.getInfo("brand"));
        stockPurchaseDetail.setUnit(reqBean.getInfo("unit"));
        stockPurchaseDetail.setNum(reqBean.getInfoInt("num"));
        stockPurchaseDetail.setOperator(reqBean.getUserId());
        stockPurchaseDetail.setRemark(reqBean.getInfo("remark"));
        return stockPurchaseDetail;
    }

}